package io.renren.socket;

import cn.hutool.http.HttpRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.util.Map;

public abstract class Middleware extends ChannelInboundHandlerAdapter{
    private Logger logger = LoggerFactory.getLogger(getClass());

    protected String name;
    //记录次数
    private int heartbeatCount = 0;

    //获取server and client 传入的值
    public Middleware(String name) {
        this.name = name;
    }
    /**
     *继承ChannelInboundHandlerAdapter实现了channelRead就会监听到通道里面的消息
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        handlerData(ctx,msg);
    }

    protected abstract void handlerData(ChannelHandlerContext ctx,Object msg);

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
            throws Exception {
        IdleStateEvent stateEvent = (IdleStateEvent) evt;

        switch (stateEvent.state()) {
            case READER_IDLE:
                handlerReaderIdle(ctx);
                break;
            case WRITER_IDLE:
                handlerWriterIdle(ctx);
                break;
            case ALL_IDLE:
                handlerAllIdle(ctx);
                break;
            default:
                break;
        }

//        if (evt instanceof HttpRequest) {
//            HttpRequest mReq = (HttpRequest) evt;
//            String clientIP = mReq.headers().get("X-Forwarded-For");
//            if (clientIP == null) {
//                InetSocketAddress insocket = (InetSocketAddress) ctx.channel()
//                        .remoteAddress();
//                clientIP = insocket.getAddress().getHostAddress();
//            }
//        }
    }

    protected void handlerAllIdle(ChannelHandlerContext ctx) {
        System.err.println("---ALL_IDLE---");
    }

    protected void handlerWriterIdle(ChannelHandlerContext ctx) {
        System.err.println("---WRITER_IDLE---");
    }


    protected void handlerReaderIdle(ChannelHandlerContext ctx) {
        System.err.println("---READER_IDLE---");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        logger.debug(" ---"+ctx.channel().remoteAddress() +"----- is  action" );
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        logger.debug(" ---"+ctx.channel().remoteAddress() +"----- is  inAction");
        String uuid = ctx.channel().id().asLongText();
        logger.debug("uuid:"+uuid);
        Map<String, Object> map = GatewayService.getChannels();
        logger.debug("map size:" + map.size());
        GatewayService.removeGatewayChannel(uuid);
    }
}